// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var constant = require("constant");
	var leftMenu = require("../leftMenu/leftMenu");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_detail = require("./pickupRansomDetail.css");
	var tools = require("tools");
	var pickupId;
	var html_oppose_pickup = require("./html_opposePickupRansomApplication.html");
	var js_platform_main = require("./platform_main.js");
	var init = function(page) {
		pickupId = page.params.pickupId;
		inquireRansomPickupApplicationDetail();
		tools.inquireCommonSelectorList(9, $(".applicantReson"));
		databind.disableInput($(".pickupDetailContent"));
		$(".left").removeClass("none");
		leftMenu.init(6);
		js_platform_main.init(0);
		initEvent();
		router.init();
		
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//拒绝提货申请
		$("#oppaseRansomPickup").on("click", function(){
			$("#popdialog").html(html_oppose_pickup);
			dialog.showDialog($("#oppose_pickup_ransom_dialog"));
			$("#pickupRansomId").val(pickupId);
			tools.inquireCommonSelectorList(9, $(".applicantReson"));
			$("#opposePickupRansomCancel").unbind("click").click(function(){
				dialog.hideDialog($("#oppose_pickup_ransom_dialog"));
			});
			$("#opposePickupRansomSure").unbind("click").click(function(){
				databind.submitDom($("#oppose_pickup_ransom_dialog"), function(){
					base.alert(1, "拒赎货申请成功！");
					dialog.hideDialog($("#oppose_pickup_ransom_dialog"));
					router.route("main/pickupRansomList.html");
				});
			});
		});
		
		//同意赎货申请
		$("#consentRansomPickup").on("click", function(){
			router.route("main/pickupRansomConsent.html?pickupId="+pickupId);
		});
	};

	//赎货申请详情
	var inquireRansomPickupApplicationDetail = function() {
		base.fnAjax({
			action: "inquireRansomPickupApplicationDetail.json",
			oData: {
				pickupId: pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					inquireFinanceApplicationDetail(data.datas[0].financeId);
					databind.fillForm($("#pickupRansomDetail"),data.datas[0]);
					if($("#pickupRansomDetailDeliveryMethod").html()==2){
						$("#pickupRansomDetailDeliveryMethod").html("到港");
					}else if($("#pickupRansomDetailDeliveryMethod").html()==1){
						$("#pickupRansomDetailDeliveryMethod").html("到门");
					}
					
					if($("#pickupRansomDetailPickupMethod").html()==1){
						$("#pickupRansomDetailPickupMethod").html("现金提货");
					}else if($("#pickupRansomDetailPickupMethod").html()==2){
						$("#pickupRansomDetailPickupMethod").html("换货");
					}
					
					juicer.register("getValueByKey", constant.getValueByKey);
					
					var tpl1 = $("#pickupRansomDetailBulkstockTpl").html();
					var html1 = juicer(tpl1, data);
					$("#pickupRansomDetailBulkstockBody").html(html1);
					
					var tpl2 = $("#pickupPreDetailInventoryTpl").html();
					var html2 = juicer(tpl2, data);
					$("#pickupPreDetailInventoryBody").html(html2);
//					
					if($("#pickupPreDetailInventoryBody").find("tr").length==1){
						$("#pickupPreDetailInventoryBody").parent().addClass("none");
					};
					if($("#pickupPreDetailBulkstockBody").find("tr").length==1){
						$("#pickupPreDetailBulkstockBody").parent().addClass("none");
					};
					
					$("#pickupPreDetailInventoryBody").on("click", ".detail", function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
					//得到货箱总估值
					$.each($(".bulkstockValuations"), function(i, val) {
							var bulkstockValuations = 0;
							$.each($(this).parents("tr").find(".bulkstockValuation"), function(i, val) {
								bulkstockValuations = bulkstockValuations + parseFloat($(this).html());
							});
							$(this).html(bulkstockValuations);
						});
						
						//得到货箱总应付值
						var allFinanceValuations = 0;
					$.each($(".financeValuations"), function(i, val) {
							var financeValuations = 0;
							$.each($(this).parents("tr").find(".financeValuation"), function(i, val) {
								financeValuations = financeValuations + parseFloat($(this).html());
							});
							$(this).html(financeValuations);
							allFinanceValuations+=financeValuations;
						});
						$(".allFinanceValuations").html(allFinanceValuations);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	//融资基本信息
	var inquireFinanceApplicationDetail = function(financeId){
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var interestDate = data.datas[0].interestDate;
//					var interestDate = new Date(interestDate);
					var myDate = new Date();
					var result=myDate.getFullYear()+'-'+(myDate.getMonth()+1)+'-'+myDate.getDate() ;
					var dateDiff1 = dateDiff(result,interestDate);
					$(".exceedingTime").html(dateDiff1+data.datas[0].loanTimeLimit);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	var  dateDiff =function(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式    
          var  aDate,  oDate1,  oDate2,  iDays    
          aDate  =  sDate1.split("-")    
          oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式    
          aDate  =  sDate2.split("-")    
          oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    
          iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数   
          return  iDays + 1   
      }
	return {
		init: init
	};
});